home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Light ROM 3
/
Light ROM 3 - Disc 2.iso
/
programs
/
amiga
/
rgb2ire
/
rgb2ire.c
< prev
Wrap
C/C++ Source or Header
|
1995-01-31
|
6KB
|
267 lines
/*
======================================================================
rgb2ire.c
Ernie Wright 30 Jan 95
SAS/C 6.51
Display NTSC composite signal amplitude in IRE, given RGB.
====================================================================== */
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/gadtools.h>
#include <intuition/gadgetclass.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int ire( int red, int green, int blue );
BOOL user( void );
void main( void )
{
user();
}
/*
======================================================================
ire()
Calculate NTSC composite signal amplitude in IRE, given RGB.
INPUTS
red, green, blue levels in [0, 255]
RESULTS
Returns the composite signal amplitude.
The inputs are scaled into [0.0, 1.0] and gamma corrected. YIQ is
found as a linear transformation using the standard matrix. The
resulting amplitude is converted to IRE units. Note that as Y goes
from 0.0 to 1.0 here, IRE level goes from 7.5 (the NTSC pedestal) to
100.0.
Based on "Television Color Encoding and 'Hot' Broadcast Colors" by
David Martindale and Alan W. Paeth, in GRAPHICS GEMS II, James Arvo
ed., Academic Press, 1991.
====================================================================== */
int ire( int red, int green, int blue )
{
double r, g, b, y, i, q, a;
r = pow(( double ) red / 255.0, 0.45 );
g = pow(( double ) green / 255.0, 0.45 );
b = pow(( double ) blue / 255.0, 0.45 );
y = 0.2989 * r + 0.5866 * g + 0.1144 * b;
i = 0.5959 * r - 0.2741 * g - 0.3218 * b;
q = 0.2113 * r - 0.5227 * g + 0.3113 * b;
a = y + sqrt( i * i + q * q );
return ( int )( 8.0 + 92.5 * a ); /* the extra 0.5 is for rounding */
}
static BOOL open_window( void );
static void close_window( void );
static void set_level( void );
static void draw_box( void );
struct Window *wnd;
struct TextAttr myfont = { "topaz.font", 8, 0, 0 };
APTR visinfo;
struct Gadget *glist, *gad[ 4 ];
int top, rgb[ 3 ], entry3[ 3 ];
BOOL user( void )
{
struct IntuiMessage msg, *pmsg;
struct Gadget *g;
BOOL done = FALSE;
if ( !open_window() ) return FALSE;
while ( !done ) {
pmsg = GT_GetIMsg( wnd->UserPort );
if ( !pmsg ) {
WaitPort( wnd->UserPort );
continue;
}
memcpy( &msg, pmsg, sizeof( struct IntuiMessage ));
GT_ReplyIMsg( pmsg );
g = ( struct Gadget * ) msg.IAddress;
switch ( msg.Class )
{
case IDCMP_REFRESHWINDOW:
GT_BeginRefresh( wnd );
draw_box();
GT_EndRefresh( wnd, TRUE );
break;
case IDCMP_CLOSEWINDOW:
done = TRUE;
break;
case IDCMP_MOUSEMOVE:
switch ( g->GadgetID ) {
case 1:
case 2:
case 3:
rgb[ g->GadgetID - 1 ] = msg.Code;
set_level();
break;
default:
break;
}
default:
break;
}
}
close_window();
return TRUE;
}
static void set_level( void )
{
int level;
level = ire( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
GT_SetGadgetAttrs( gad[ 3 ], wnd, NULL,
GTNM_Number, level,
TAG_DONE );
SetRGB4( &wnd->WScreen->ViewPort, 3,
rgb[ 0 ] >> 4,
rgb[ 1 ] >> 4,
rgb[ 2 ] >> 4 );
}
static BOOL open_window( void )
{
char *gtext[ 3 ] = { "R", "G", "B" };
struct NewGadget ng;
struct Gadget *g;
struct Screen *s;
int i;
s = LockPubScreen( NULL );
if ( !s ) return FALSE;
visinfo = GetVisualInfo( s, TAG_DONE );
top = s->WBorTop + s->Font->ta_YSize + 1;
i = GetRGB4( s->ViewPort.ColorMap, 3 );
entry3[ 0 ] = ( i & 0xF00 ) >> 8,
entry3[ 1 ] = ( i & 0x0F0 ) >> 4,
entry3[ 2 ] = i & 0x00F;
g = CreateContext( &glist );
ng.ng_LeftEdge = 45;
ng.ng_Width = 261;
ng.ng_Height = 14;
ng.ng_TextAttr = &myfont;
ng.ng_Flags = PLACETEXT_RIGHT;
ng.ng_VisualInfo = visinfo;
ng.ng_UserData = NULL;
for ( i = 0; i < 3; i++ ) {
rgb[ i ] = entry3[ i ] * 17;
ng.ng_TopEdge = top + 34 + i * 18;
ng.ng_GadgetText = gtext[ i ];
ng.ng_GadgetID = i + 1;
gad[ i ] = g = CreateGadget( SLIDER_KIND, g, &ng,
GTSL_Min, 0,
GTSL_Max, 255,
GTSL_Level, rgb[ i ],
GTSL_LevelFormat, "%3ld",
GTSL_MaxLevelLen, 3,
GTSL_LevelPlace, PLACETEXT_LEFT,
PGA_Freedom, LORIENT_HORIZ,
TAG_DONE );
}
i = ire( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
ng.ng_LeftEdge = 142;
ng.ng_TopEdge = top + 14;
ng.ng_Width = 24;
ng.ng_Height = 8;
ng.ng_GadgetText = "IRE";
ng.ng_GadgetID = 4;
ng.ng_Flags = PLACETEXT_RIGHT;
gad[ 3 ] = g = CreateGadget( NUMBER_KIND, g, &ng,
GTNM_Number, i,
TAG_DONE );
if ( !g ) {
close_window();
return FALSE;
}
wnd = OpenWindowTags( NULL,
WA_Left, 160,
WA_Top, 75,
WA_Width, 340,
WA_Height, 110,
WA_IDCMP,
SLIDERIDCMP |
IDCMP_CLOSEWINDOW |
IDCMP_REFRESHWINDOW,
WA_Flags,
WFLG_DRAGBAR |
WFLG_CLOSEGADGET |
WFLG_DEPTHGADGET |
WFLG_SIMPLE_REFRESH |
WFLG_ACTIVATE,
WA_Gadgets, glist,
WA_Title, "RGB to IRE",
WA_PubScreen, s,
TAG_DONE );
if ( !wnd ) {
close_window();
return FALSE;
}
UnlockPubScreen( NULL, s );
GT_RefreshWindow( wnd, NULL );
draw_box();
return TRUE;
}
static void draw_box( void )
{
static struct Image box = { 0, 0, 80, 20, 2, NULL, 0, 3, NULL };
DrawImage( wnd->RPort, &box, 47, top + 7 );
DrawBevelBox( wnd->RPort, 45, top + 6, 84, 22,
GT_VisualInfo, visinfo,
GTBB_Recessed, TRUE,
TAG_DONE );
}
static void close_window( void )
{
if ( wnd ) {
SetRGB4( &wnd->WScreen->ViewPort, 3,
entry3[ 0 ], entry3[ 1 ], entry3[ 2 ] );
CloseWindow( wnd );
}
if ( glist ) FreeGadgets( glist );
if ( visinfo ) FreeVisualInfo( visinfo );
}